//*********************************************
//ADRIAN SALAZAR && JUSTIN MICHALE SCHWARTZBECK
//HOMEWORK 2
//FEB. 23, 2012
//
//Request: This is the client that will create a
//	new hash and send it to the server to break
//	it will print out the results. 
//*********************************************


#include "lex.h"
#include "lsp.h"
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <openssl/sha.h>
#define MAX 4096

int main(int argc, char** argv) 
{
	//Default variables
	int port = 8016;
	char * ip = "127.0.0.1";
	unsigned char message[41] ; //= "cat";
	char * hash = new char[20];
	memset(hash, 0, 20);
	int len;
	unsigned char new_msg[MAX];
	//Initialize
	//Break down the command line arguments
	if(argc > 1)
	{
		ip = strtok(argv[1], ":");
		if(ip != NULL)
		{
			port = atoi(strtok(NULL, " ")); 
		}
		else
		{
			printf("The correct format is host:port\n");
			return -1;
		}
	}
	
	if(argc > 2) memcpy(message, argv[2], strlen(argv[2])+1);
	if(argc > 3) len = atoi(argv[3])+1;
	
	//printf("HASH: %s\n", message);

	//Generate uper lower passwords to try
	char lower[len];
	char upper[len];
	for(int i = 0; i < len - 1; i++)
	{	lower[i] = 'a';
		upper[i] = 'z';
	}
	//Append the null char
	lower[len - 1] = '\0';
	upper[len - 1] = '\0';
	
	
	//Generate using hash
	//SHA1((const unsigned char *)message, strlen((const char *)message), (unsigned char *)hash);
	//hash = hash_to_str((unsigned char*)hash);

	//Create a connection to the server
	lsp_client * req_client = lsp_client_create(ip, port);

	//Send packets to the server
	unsigned char init_msg[] = "c";
	unsigned char tok[] = ":";
	
	
	strcat((char *)new_msg, (const char *)init_msg);
	strcat((char *)new_msg, (const char *)tok);
	strcat((char *)new_msg, (const char *)message);
	strcat((char *)new_msg, (const char *)tok);
	strcat((char *)new_msg, (const char *)lower);
	strcat((char *)new_msg, (const char *)tok);
	strcat((char *)new_msg, (const char *)upper);
//	strcat((char *)new_msg, (const char *)"\n");

//	printf("NEW MSG: %s\n", new_msg);
// 	lsp_client_write(req_client, (uint8_t *) init_msg, strlen((const char *)init_msg));
// 	lsp_client_write(req_client, (uint8_t *) hash, strlen((const char *)hash));
// 	lsp_client_write(req_client, (uint8_t *) upper, strlen(upper)-1);
// 	lsp_client_write(req_client, (uint8_t *) lower, strlen(lower)-1);
// 	
	lsp_client_write(req_client, (uint8_t *) new_msg, strlen((const char *)new_msg));
	
	//printf("after writing\n");
	//Receive answer from the server
	uint8_t buffer[4096];
	char buffer2[MAX];
 	int bytes_read = lsp_client_read(req_client, (uint8_t *) buffer);
	
	strncpy((char *)buffer2, (const char *)buffer, 1);

	//Interpret Results
	if(bytes_read < 0)
	{
		printf("Disconnected\n");
	}
	else if(strcmp((const char *)buffer2, "f")==0)
	{
		printf("Found: ");
		for(int i = 2; i < strlen((const char *)buffer); i++)
			printf("%c", buffer[i]);
		printf("\n");
	}
	else
	{
		printf("Not Found\n");
	}
	//printf("Shutting Down Request Client\n");
	lsp_client_close(req_client);
	return 0;
}
